<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 活动
 * Class Activity
 * @package app\api\controller
 */
class Activity extends Api
{

	use \app\common\traits\ToolTrait;

	protected $noNeedLogin = ['descrip'];
	protected $noNeedRight = '*';

	/**
	 * 说明
	 */
	public function descrip()
	{
		$uid = $this->auth->id;
		$dao = new \app\common\model\Activity;
		$finds = $dao->where("typs", 1)
			->where(["is_open" => 1, "status" => 1])
			->field("id,title,days,stands_money standMoney,maxNum,remark,typs")
			->find();
		$finds = $finds->toArray();
		if ($finds) {

			$finds["standMoney"] = bcmul((string)$finds["standMoney"], '10000', 2);

			$finds["shuoming"] = "活动名额有限，参与人数达到上限时将不再发放参与资格！";
			$finds["guize"] = "新用户注册" . $finds["days"] . "天之内交易达到" . $finds["standMoney"] . "有达标礼品！起始日期以参与日期为准";
			$finds["isTake"] = $this->takes($uid, $finds["typs"]) ? 1 : 0;
			$finds["takeNo"] =  $this->takeNo($uid, $finds["typs"]);
			// 我的
			$finds["mine"] = [];
			$user = $this->user($uid);
			if ($user) {
				$temp = [];
				// tranAmt,is_stands isStands
				$takeModel = new \app\common\model\ActivityTake;
				$takes = $takeModel->where("uid", $uid)->where("act_id", $finds["id"])
					->field("standsAmt stanAmt,tranAmt,is_stands isStands,from_unixtime(start , '%Y-%m-%d') start,from_unixtime(ends , '%Y-%m-%d') ends")
					->find();
				if ($takes) {
					$temp = $takes->toArray();
				} else {
					$stanAmt = $finds["standMoney"];
					$ends = strtotime("-1 day", strtotime("+" . $finds["days"] . " day", $user->jointime));
					$tranAmt = $this->trans($uid, $user->jointime, $ends);
					$temp["stanAmt"] = $stanAmt;
					$temp["start"] = date("Y-m-d", $user->jointime);
					$temp["ends"] = date("Y-m-d", $ends);
					$temp["tranAmt"] = $tranAmt;
					$temp["isStands"] = ($tranAmt >= $stanAmt) ? 1 : 2;
				}
				$finds["mine"] = $temp;
			}
			//  达标
		}
		$this->success("新人活动礼", $finds);
	}


	/**
	 * 已达标
	 */
	public function stands_list()
	{
		$typs = $this->request->post("typs", 1);
		$data = [];
		$dao = new \app\common\model\ActivityTake;
		$list = $dao->with("user")
			->where("active_typs", $typs)
			->where("is_stands", 1)
			->field("uid,user.avatar")
			->select();
		$list = collection($list)->toArray();
		if ($list) {
			foreach ($list as $k => $v) {
				$temp = [];
				$temp["imgUrl"] =  "http://" . $_SERVER["HTTP_HOST"] . ($v['avatar'] ? $v["avatar"] : "/assets/img/avatar.png");
				array_push($data, $temp);
			}
		}
		$ret = $this->xuni($this->rand_nums(2));
		foreach ($ret as $val) {
			$xun = [];
			$xun["imgUrl"] = "http://" . $_SERVER["HTTP_HOST"] . $val["headimg"];
			array_push($data, $xun);
		}
		$total = count($data);
		// $pageArr = $this->array_page($data, 1, 20);
		$this->success("达标列表", ["data" => $data, "total" => $total]);
	}

	/**
	 * 参与活动
	 */
	public function take_part()
	{
		$uid = $this->auth->id;
		$actid = $this->request->post("actid", 0);
		if (!$actid) {
			$this->error(__('Invalid parameters'));
		}
		$dao = new \app\common\model\Activity;
		$template = $dao->where("id", $actid)
			->field("typs,days,stands_money,is_open,maxNum")
			->find()->toArray();
		$check = $this->takes($uid, $template['typs']);
		if (!$check) {
			$user = $this->user($uid);
			if ($user) {
				$data = [];
				$start = strtotime(date("Y-m-d", time()));
				$ends = strtotime("-1 day", strtotime("+" . $template["days"] . " day", $start));
				$data = [
					"uid" 			=> $uid,
					"active_typs" 	=> $template["typs"],
					"start" 		=> $start,
					"ends" 			=> $ends,
					"standsAmt"		=> bcmul((string)$template["stands_money"], '10000', 2),
					"create_time"	=> time(),
					"act_id"		=> $actid

				];
				$dao = new \app\common\model\ActivityTake;
				$id = $dao->insertGetId($data);
				if ($id) {
					$this->success('参与成功', 1);
				} else {
					$this->error("参与失败");
				}
			} else {
				$this->error("客户未找到");
			}
		} else {
			$this->error(__('已参与活动！！！'));
		}
	}

	/**
	 * 交易
	 */
	public function trans($uid, $start, $ends)
	{
		$sum = "0.00";
		$dao = new \app\common\model\TransDaysCustomer;
		$where = "(times >= {$start} and times <= {$ends})";
		$ret = $dao->where("id", $uid)->where($where)->sum("money");
		if ($ret) $sum = $ret;
		return $sum;
	}

	/**
	 * 客户信息
	 */
	public function user($uid)
	{
		$dao = new \app\common\model\User;
		return $dao->where("id", $uid)->find();
	}

	/**
	 * 参与人数
	 */
	public function takeNo($uid, $typs)
	{
		$dao = new \app\common\model\ActivityTake;
		$real = $dao->where("active_typs", $typs)->count();
		$xuni = count($this->xuni($this->rand_nums(1)));
		return bcadd((string)$real, (string)$xuni, 0);
	}

	/**
	 * 虚拟
	 */
	public function xuni($limit)
	{
		$sql = "SELECT * FROM `fd_user_xuni_wx_img`  LIMIT {$limit}";
		return Db::query($sql);
	}

	/**
	 * 随机数
	 */
	public function rand_nums($t)
	{
		return Db::name("activity_take_nums")->where("status", $t)->value("nums");
	}

	/**
	 * 参与信息
	 */
	public function takes($uid, $typs)
	{
		$dao = new \app\common\model\ActivityTake;
		return $dao->where("uid", $uid)->where("active_typs", $typs)->find();
	}
}
